<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="品牌" prop="brand">
        <el-input v-model="queryParams.brand" placeholder="请输入品牌" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="物品名称" prop="goodsName">
        <el-input v-model="queryParams.goodsName" placeholder="请输入物品名称" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="公司名称" prop="company">
        <el-input v-model="queryParams.company" placeholder="请输入公司名称" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="类型名称" prop="typeName">
        <el-input v-model="queryParams.typeName" placeholder="请输入类型名称，与回收类型表关联" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['business:barcodeLibrary:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['business:barcodeLibrary:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['business:barcodeLibrary:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['business:barcodeLibrary:export']">导出</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="info" plain icon="el-icon-download" size="mini" @click="handSubmitTo"
          v-hasPermi="['business:barcodeLibrary:export']">导入</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="barcodeLibraryList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <!-- <el-table-column label="主键id" align="center" prop="id" /> -->
      <el-table-column label="排序" align="center" type="index" />
      <el-table-column label="条形码值" align="center" prop="barcode" />
      <el-table-column label="品牌" align="center" prop="brand" />
      <el-table-column label="物品名称" align="center" prop="goodsName" />
      <el-table-column label="公司名称" align="center" prop="company" />
      <el-table-column label="包装材料" align="center" prop="packagingType" />
      <el-table-column label="类型名称" align="center" prop="typeName" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['business:barcodeLibrary:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['business:barcodeLibrary:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改条形码库对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="品牌" prop="brand">
          <el-input v-model="form.brand" placeholder="请输入品牌" />
        </el-form-item>
        <el-form-item label="条形码值" prop="barcode">
          <el-input v-model="form.barcode" placeholder="请输入条形码值" />
        </el-form-item>
        <el-form-item label="包装材料" prop="packagingType">
          <el-input v-model="form.packagingType" placeholder="请输入包装材料" />
        </el-form-item>
        <el-form-item label="物品名称" prop="goodsName">
          <el-input v-model="form.goodsName" placeholder="请输入物品名称" />
        </el-form-item>
        <el-form-item label="公司名称" prop="company">
          <el-input v-model="form.company" placeholder="请输入公司名称" />
        </el-form-item>
        <el-form-item label="类型名称" prop="typeName">
          <el-select v-model="form.typeName" placeholder="请选择回收分类" clearable style="width: 100%">
            <el-option v-for="dict in recycleTypeList" :key="dict.id" :label="dict.recycleName"
              :value="dict.recycleName" />
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <el-dialog :title="title" :visible.sync="show" width="500px" append-to-body>
      <el-upload class="upload-demo"  ref="upload" drag action="http://hs.api.e0s.cn/prod-api/business/barcodeLibrary/importBarcodes"
        :on-change="onFile" :auto-upload="false" accept="xlsx" :data="{'updateSupport':true}" :headers="{Authorization:'Bearer ' + token}">
        <i class="el-icon-upload"></i>
        <div class="el-upload__text">将文件拖到此处，或<em>点击上传</em></div>
        <div class="el-upload__tip" slot="tip">只能上传xlsx文件，且不超过500kb</div>
      </el-upload>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="setFile()">确 定</el-button>
        <el-button @click="show = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listBarcodeLibrary, getBarcodeLibrary, delBarcodeLibrary, addBarcodeLibrary, updateBarcodeLibrary } from "@/api/business/barcodeLibrary";
import { listRecycleType } from "@/api/business/recycleType";
import { getToken } from '@/utils/auth'

export default {
  name: "BarcodeLibrary",
  data() {
    return {
      // 根路径
      baseURL: process.env.VUE_APP_BASE_API,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // token
      token:getToken(),
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 条形码库表格数据
      barcodeLibraryList: [],
      //类型
      recycleTypeList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      show: false,
      // 上传的二进制文件
      file: null,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        brand: null,
        goodsName: null,
        company: null,
        packagingType: null,
        typeName: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        id: [
          { required: true, message: "主键id不能为空", trigger: "blur" }
        ],
        barcode: [
          { required: true, message: "条形码值不能为空", trigger: "blur" }
        ],
        brand: [
          { required: true, message: "品牌不能为空", trigger: "blur" }
        ],
        goodsName: [
          { required: true, message: "物品名称不能为空", trigger: "blur" }
        ],
        company: [
          { required: true, message: "公司名称不能为空", trigger: "blur" }
        ],
        packagingType: [
          { required: true, message: "包装材料不能为空", trigger: "change" }
        ],
        typeName: [
          { required: true, message: "类型名称，与回收类型表关联不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
    this.getTypeList();
  },
  methods: {
    /** 查询条形码库列表 */
    getTypeList() {
      this.loading = true;
      listRecycleType({
        pageNum: 1,
        pageSize: 99,
      }).then(response => {

        let arr = []
        response.rows.forEach(res => {
          if (res.parentId != 0) {
            arr.push(res)
          }
        })
        this.recycleTypeList = arr;
      });

    },
    getList() {
      this.loading = true;
      listBarcodeLibrary(this.queryParams).then(response => {
        this.barcodeLibraryList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        barcode: null,
        brand: null,
        goodsName: null,
        company: null,
        packagingType: null,
        typeName: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加条形码库";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getBarcodeLibrary(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改条形码库";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateBarcodeLibrary(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addBarcodeLibrary(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除条形码库编号为"' + ids + '"的数据项？').then(function () {
        return delBarcodeLibrary(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('business/barcodeLibrary/export', {
        ...this.queryParams
      }, `barcodeLibrary_${new Date().getTime()}.xlsx`)
    },
    handSubmitTo() {
      this.show = true
    },
    onFile(file) {
      this.file = file.row
      console.log(file.row);
    },
    setFile(){
      this.$refs.upload.submit();
      this.show = false
    }
  }
};
</script>
